<template>
  <GlobalContainer className="!bg-[#F3F3F3]" :backgroundImage="`${OSS_URL}/images/jiaofei/jiaofei_bg.png`">
    <GlobalHeader ref="headerRef" title="缴费记录" :fixed="true" showBack :backgroundImage="`${OSS_URL}/images/jiaofei/jiaofei_bg.png`" />
    <div class="p-24">
      <div class="status-bar-container fixed" :style="{ top: `${headerHeight}` }">
        <div
          class="status-bar-item"
          v-for="(item, index) in statusBarList"
          :key="index"
          :class="statusBarActive === item.value && 'status-bar-item-active'"
          @tap="getOrderList(item.value)"
        >
          {{ item.name }}
        </div>
      </div>

      <div class="payment-records" :style="{ paddingTop: '100rpx' }">
        <div class="payment-item" v-for="(item, index) in orderList" :key="index" @tap="toJiaofeiDetailHandle(item.id)">
          <div class="line1 flex items-center">
            <div class="text-30 text-[#303133]">{{ accountStore.roomInfo }}</div>
            <div class="right ml-auto">
              <image class="size-33" src="../../static/images/jiaofei_right_icon.png" />
            </div>
          </div>
          <div class="line2 mt-20 flex items-center">
            <div class="status status-cancel" :class="statusClassMap[item.status]">{{ statusMap[item.status] }}</div>
            <div class="ml-auto text-34 leading-52 text-[#303133]">￥{{ item.payPrice }}</div>
          </div>

          <div class="line3 mt-20 text-28 font-normal text-[#909399]">
            <div>{{ item.orderNo }}</div>
            <div class="mt-20">{{ item.createTime }}</div>
          </div>

          <div class="line4 mt-24 flex items-center">
            <!-- <div class="text-28 text-[#303133]">
              <span class="font-normal">支付剩余</span>
              <span class="ml-10 text-[#CC000A]">14:29</span>
            </div> -->
            <div class="right ml-auto flex items-center" v-if="item.status === 10">
              <div class="btn cancel-btn" @tap.stop="cancelOrderHandle(item.id)">取消订单</div>
              <div class="btn pay-btn" @tap.stop="payHandle(item.orderNo)">立即支付</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div v-if="orderList.length === 0" class="empty-container mx-auto mt-240 flex flex-col items-center">
      <div class="empty-img mb-40">
        <image class="h-240 w-330" :src="`${OSS_URL}/images/jiaofei_empty.png`" />
      </div>
      <div class="empty-text text-28 leading-36 text-[#909399]">暂无记录</div>
    </div>
    <div v-else class="empty-container mt-18 text-center text-28 leading-36 text-[#909399]">已加载全部记录</div>
  </GlobalContainer>
</template>

<script setup>
import { ref, computed } from 'vue'
import { getPropertyOrderList, propertyCancelOrder, propertyPayment } from '@/common/api/order'
import { showLoading, hideLoading } from '@/utils'
import { useAccountStore } from '@/store'
const accountStore = useAccountStore()
const OSS_URL = import.meta.env.VITE_OSS_URL

const headerRef = ref()
const headerHeight = computed(() => {
  return headerRef.value?.wrapStyle?.height
})
const statusBarList = ref([
  {
    name: '全部',
    value: ''
  },
  {
    name: '已支付',
    value: 20
  },
  {
    name: '未支付',
    value: 10
  },
  {
    name: '已取消',
    value: 0
  }
])
// 0-已关闭 10-待支付 20-待服务 25-申请退款 30-待验收 40-已完成 60-已退款
const statusMap = {
  10: '未支付',
  20: '已支付',
  0: '已取消'
}
const statusClassMap = {
  10: 'status-wait',
  20: 'status-pay',
  0: 'status-cancel'
}
const statusBarActive = ref('')

const orderList = ref([])
const getOrderList = status => {
  statusBarActive.value = status
  showLoading()
  getPropertyOrderList({
    status: statusBarActive.value,
    pageNo: 1,
    pageSize: 9999
  })
    .then(res => {
      orderList.value = res.records || []
    })
    .finally(() => {
      hideLoading()
      uni.stopPullDownRefresh()
    })
}

const cancelOrderHandle = id => {
  propertyCancelOrder(id).then(res => {
    if (res) {
      uni.showToast({
        title: '取消成功',
        icon: 'success'
      })
      getOrderList(statusBarActive.value)
    }
  })
}
const payHandle = orderNo => {
  propertyPayment({
    orderNo: orderNo
  }).then(res => {
    if (res.prepayId) {
      uni.requestPayment({
        provider: 'wxpay',
        timeStamp: String(res.timeStamp),
        nonceStr: res.nonceStr,
        package: 'prepay_id=' + res.prepayId,
        signType: 'SHA256withRSA',
        paySign: res.paySign,
        orderInfo: {
          appId: '',
          timeStamp: String(res.timeStamp),
          nonceStr: res.nonceStr,
          package: ''
        },
        success: function (res) {
          getOrderList(statusBarActive.value)
        },
        complete: function () {}
      })
    }
  })
}
const toJiaofeiDetailHandle = id => {
  uni.navigateTo({
    url: '/subPages/jiaofeiLogDetail/index?id=' + id
  })
}

onShow(() => {
  getOrderList(statusBarActive.value)
})
onPullDownRefresh(() => {
  getOrderList(statusBarActive.value)
})
</script>

<style scoped lang="scss">
.status-bar-container {
  width: 702rpx;
  left: 50%;
  transform: translateX(-50%);
  height: 80rpx;
  line-height: 1;
  text-align: center;
  background: #ffffff;
  border-radius: 10rpx 10rpx 10rpx 10rpx;
  padding: 24rpx 0;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.status-bar-item {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #fff;
  font-weight: 400;
  font-size: 28rpx;
  color: #606266;
  border-right: 1px solid #e3e7ec;
}
.status-bar-item-active {
  color: #1e90ff;
}

.payment-records {
  .payment-item {
    margin-bottom: 20rpx;
    background: #fcfeff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    padding: 30rpx 42rpx 30rpx 32rpx;
    border: 1rpx solid #ffffff;
    overflow: hidden;
    .status {
      width: 118rpx;
      height: 52rpx;
      text-align: center;
      line-height: 52rpx;
      font-weight: 400;
      font-size: 26rpx;
      color: #ffffff;
      box-shadow: inset 0rpx 8rpx 16rpx 0rpx rgba(255, 255, 255, 0.3);
      border-radius: 12rpx 0rpx 12rpx 0rpx;
    }
    .status-wait {
      background: linear-gradient(303deg, #ff9933 0%, #ff8000 100%);
    }
    .status-cancel {
      background: linear-gradient(303deg, #a3b4cc 0%, #8fa1b2 100%);
    }
    .status-pay {
      background: linear-gradient(303deg, #3389ff 0%, #339aff 100%);
    }
  }
  .btn {
    width: 160rpx;
    height: 60rpx;
    line-height: 60rpx;
    text-align: center;
    border-radius: 40rpx 40rpx 40rpx 40rpx;
    font-size: 26rpx;
    color: #ffffff;
    border: 1px solid;
  }
  .cancel-btn {
    color: #606266;
    border-color: #cdd0d6;
  }
  .pay-btn {
    margin-left: 20rpx;
    background: #1e90ff;
  }
}
</style>
